package com.qing.thread.create;

import com.qing.thread.util.DownloadPicUtil;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 方式3：实现Callable类
 * 1：重写call()方法
 * 2：添加返回值类型
 * 3：//创建实现Callble的对象
 * CreateThread3 t1 = new CreateThread3
 * //创建执行服务
 * ExecutorService executorService = Executors.newFixedThreadPool(4);
 * //提交执行
 * executorService.submit(t1);
 * //关闭服务
 * executorService.shutdown();
 */
public class CreateThread3 implements Callable<Boolean> {
    private String url;
    private String name;

    public CreateThread3(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public Boolean call() {
        new DownloadPicUtil().downloadPic(url, name);
        System.out.println(name + "下载完咯！");
        return true;
    }


    public static void main(String[] args) {
        CreateThread3 t1 = new CreateThread3("https://lmg.jj20.com/up/allimg/1113/052420110515/200524110515-1-1200.jpg", "雪山.jpg");
        CreateThread3 t2 = new CreateThread3("https://img1.baidu.com/it/u=481689849,3217240113&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500", "大海.jpg");
        CreateThread3 t3 = new CreateThread3("https://img2.baidu.com/it/u=2529031386,4198239090&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500", "落日.jpg");
        CreateThread3 t4 = new CreateThread3("https://img2.baidu.com/it/u=3812026306,1183665387&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500", "蓝天.jpg");

        //创建执行服务
        ExecutorService executorService = Executors.newFixedThreadPool(4);
        //提交执行
        Future<Boolean> rs1 = executorService.submit(t1);
        Future<Boolean> rs2 = executorService.submit(t2);
        Future<Boolean> rs3 = executorService.submit(t3);
        Future<Boolean> rs4 = executorService.submit(t4);

        //关闭服务
        executorService.shutdown();
    }
}
